Imports Elmah
Imports NENAData.NENAMember

Partial Class Disclaimer
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim strReferralId As String = String.Empty
        strReferralId = Request.QueryString("ReferralId")
        If Not strReferralId Is Nothing Then
            txt_ReferralCode.Text = strReferralId
            Session("ReferralId") = strReferralId
        Else
            Session("ReferralId") = ""
        End If
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAgree.Click
        'Response.Redirect("applicationform.aspx")
        CheckForAccount()
    End Sub

    Private Sub CheckForAccount()
        Dim IntNenaMembershipId As Integer

        IntNenaMembershipId = FunctionsSecurity.ReturnPaidMembershipId_byEmail(PersonalEmail.Text)
        'Response.Write(strAccountExists)
        PersonalEmail.Enabled = False

        Select Case IntNenaMembershipId
            Case -1
                'never paid
                btnAgree.Visible = False
                btnRenewal.Visible = False
                Dim oNenaData As NENAMember = New NENAMember
                Dim oMember = oNenaData.GetMember(PersonalEmail.Text)
                'http://membership.nena.ca
                lblHeaderMessage.Text = "Your email already exists in our database but we have not recieved your payment.<br/>" & _
                    "<a href='/applicationformcompleted.aspx?" & _
                    "Member_ID=" & oMember.Member_ID & _
                    "&Affiliate_ID=" & oMember.Affiliate_ID & "'>Click this link to review your membership payment instructions. <br />"
                '                    "Click 'Resend Payment Info' below to get the payment details.<br/>" & _
                '"If you prefer to fill out a new application click the 'New Application' link<br/>"
            Case 0
                'new applicant
                Session("newEmail") = PersonalEmail.Text
                Response.Redirect("applicationform.aspx", False)
            Case Is > 0
                'renewing user
                Dim intCommunityServerId As Integer
                intCommunityServerId = FunctionsSecurity.ReturnCommunityServerUserId(IntNenaMembershipId)
                If intCommunityServerId > 0 Then
                    lblHeaderMessage.Text = "We have a website account with this email address.<br/>" & _
                        "Your NENA membership may not be current in standing. <br/><br/>" & _
                        "<a href='http://nena.ca/user/EmailForgottenPassword.aspx'>1. Click this link to retrieve your user name and password</a> " & _
                        "<br /> <a href='/renew/'>2. Click this link to review or renew your account. <br /> "
                    btnAgree.Visible = False
                    btnRenewal.Visible = True
                    'btnPasswordReset.Visible = True
                Else
                    lblHeaderMessage.Text = "Your email already exists in our database.<br/>" & _
                        "We need to create an account for you on our new website. <br/>" & _
                        "Please click the <b>'Create Account'</b> button and we will email you your username and password."
                    btnAgree.Visible = False
                    btnRenewal.Visible = False
                    btnCreateAccount.Visible = True
                    txtMemberId.Text = IntNenaMembershipId
                End If
        End Select
    End Sub
    Protected Sub btnRenewal_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnRenewal.Click
        Response.Redirect("/renew", False)
    End Sub
    Protected Sub btnPasswordReset_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnPasswordReset.Click
        Response.Redirect("http://nena.ca/user/EmailForgottenPassword.aspx", False)
    End Sub
    Protected Sub btnCreateAccount_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCreateAccount.Click
        Try
            Dim CsUser As NENAData.NENAMember.NENAMember = New NENAData.NENAMember.NENAMember
            Dim intApplyingMemberId As Integer = CType(txtMemberId.Text, Integer)
            Dim boolUniqueUser As Boolean = CsUser.UniqueCsUserName(intApplyingMemberId)
            If Not boolUniqueUser Then
                Dim oApplyingMember = CsUser.GetMember(intApplyingMemberId)

                Dim strAccountName As String = oApplyingMember.LastName + oApplyingMember.FirstName
                strAccountName = strAccountName.Replace(" ", "")
                Dim oOrginalMember = CsUser.GetMember_byCsUserName(strAccountName)
                NENAData.Email.Message.DuplicateAccount(intApplyingMemberId)
                Dim sbMessage As New StringBuilder
                sbMessage.Append("A member already exists with the same name at this email address ").Append(oOrginalMember.EmailAddress)
                sbMessage.Append("<br /> If this is your email address please apply using this address. <br /><br />")
                sbMessage.Append("If this is not your email address, please contact us at WebSiteAdministrator@nena.ca")
                lblHeaderMessage.Text = sbMessage.ToString
                btnCreateAccount.Visible = False
                Exit Sub
            End If

            CsUser.CreateTelligentCommunityUser(intApplyingMemberId)
            lblHeaderMessage.Text = "Your account has been created <br/>" & _
                "Please check your email for your account information, it could take a few minutes for the email to arrive."
            btnCreateAccount.Visible = False
        Catch ex As Exception
            lblHeaderMessage.Text = "We are sorry, an error occured.<br/>" & _
                "We have been notified by email and we will look into this error."
        End Try
    End Sub
End Class
